﻿using Client.Event;
using Client.ICommandNotify;
using Client.Models;
using Mysqlx.Prepare;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Client.ViewModel.CBM
{
    public class CBMViewModel : NotifyBase
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CBMViewModel(EventExample eventExample)
        {
            icommand = new Command(queryDoExecute, queryCanExecute);
            SendEsa = new Command(SendEsaDoExecute, DoExecuteCanExecute);
            eventExample.SimpleEvent += EventExample_SimpleEvent;
        }

        private bool DoExecuteCanExecute()
        {
            return true;
        }

        private async void SendEsaDoExecute(object obj)
        {
            var http = HelpTools.IOC.Getobject<HttpHelp.Http>();
            var data=await  http.GetData("http://cpms_cbm.scrbg.com/api/pmkd/SendEsa");
            WPFDevelopers.Minimal.Controls.MessageBox.Show(data, "消息", MessageBoxButton.OK, MessageBoxImage.Information);

        }

        private void EventExample_SimpleEvent(object sender)
        {
            dataGrid= sender as DataGrid;
        }
        #region 方法
        private bool queryCanExecute()
        {
            return string.IsNullOrWhiteSpace(DocumentNumber) && string.IsNullOrWhiteSpace(SelectText) ? false : true;
        }

        private async void queryDoExecute(object obj)
        {
            var Iconfig  =HelpTools.IOC.Getobject<IConfiguration.IConfigurationHelp>();
            var Dialog_ = HelpTools.IOC.Getobject<MessageDialog.Dialog>();
            var DbContext_ = HelpTools.IOC.Getobject<SqlServer.DbContext>();
            string? connstr=  Iconfig?.GetSetUpModel()?.Where(o => o.DatabaseName == Title)?.FirstOrDefault()?.ConnectingString;
            if (string.IsNullOrWhiteSpace(connstr))
            {
                Dialog_.OpenMessage("数据连接字符串为空,请检查数据库配置是否正确");
                return;
            }
            DbContext_.SqlConfigstr = connstr;
            Dialog_.OpenDialog=true;
             await Task.Delay(2000);
           var da= await  Task.Run(() =>
            {
                
                object cbmdatadtl = new object();
                var  cbmdata=  DbContext_.Where<CBM_Contract>($"SELECT a.BillId,a.BillNo,a.Amount,a.SettledAmount,a.Type FROM {SelectText} a WHERE BillNo= '{DocumentNumber}'");
                if (cbmdata.Any())
                {
                    switch (cbmdata.FirstOrDefault()?.Type)
                    {
                        case 4:
                            cbmdatadtl = DbContext_.Where<CBM_MaterialPurchaseList>($"SELECT *FROM CBM_MaterialPurchaseList  WHERE BillId = '{cbmdata?.FirstOrDefault()?.BillId}' ");
                            break;
                        case 8:
                            cbmdatadtl = DbContext_.Where<EquipmentLeaseList>($"SELECT *FROM CBM_EquipmentLeaseList WHERE BillId = '{cbmdata?.FirstOrDefault()?.BillId}' ");
                            break;
                        default:
                            break;
                    }
                }
                    
                return new
                {
                    cbmdata = cbmdata,
                    cbmdatadtl=cbmdatadtl,
                    Type= cbmdata?.FirstOrDefault()?.Type
                };
            });
            Dialog_.OpenDialog = false;
           
            CBM_ContractList = da.cbmdata;
            switch (da.Type)
            {
                case 4:
                    if (dataGrid != null)
                    {
                        dataGrid.Columns.Clear();
                        Type entityType = typeof(CBM_MaterialPurchaseList);
                        PropertyInfo[] properties = entityType.GetProperties();
                        foreach (PropertyInfo property in properties)
                        {
                            Type propertyType = property.PropertyType;
                            DisplayAttribute displayAttribute = (DisplayAttribute)property.GetCustomAttribute(typeof(DisplayAttribute), false);
                            dataGrid.Columns.Add(new DataGridTextColumn()
                            {
                                Header = displayAttribute.Name,
                                Binding = new Binding(property.Name)
                            });
                        }
                        dataGrid.ItemsSource = da.cbmdatadtl as List<CBM_MaterialPurchaseList>;
                    }
                    break;
                case 8:
                    if (dataGrid != null)
                    {
                        dataGrid.Columns.Clear();
                        Type entityType = typeof(EquipmentLeaseList);
                        PropertyInfo[] properties = entityType.GetProperties();
                        foreach (PropertyInfo property in properties)
                        {
                            Type propertyType = property.PropertyType;
                            DisplayAttribute displayAttribute = (DisplayAttribute)property.GetCustomAttribute(typeof(DisplayAttribute), false);        
                            dataGrid.Columns.Add(new DataGridTextColumn()
                            {
                                Header= displayAttribute.Name,
                                Binding=new Binding(property.Name)
                            });
                        }
                        dataGrid.ItemsSource = da.cbmdatadtl as List<EquipmentLeaseList>;
                    }
                    break;
                default:
                    break;
            }
    
           
        }
        #endregion
        #region 命令

        public Command icommand { get; set; }

        public Command SendEsa {  get; set; }
        #endregion
        #region 属性
        private string Title_ = "合同";
        //标题
        public string Title
        {
            get { return Title_; }
            set { Title_ = value; }
        }
        public DataGrid? dataGrid { get; set; }
        private string DocumentNumber_;
        /// <summary>
        /// 单据数据
        /// </summary>
        public string DocumentNumber
        {
            get { return DocumentNumber_; }
            set { DocumentNumber_ = value;
                SetProperty(ref DocumentNumber_, DocumentNumber);
            }
        }
        /// <summary>
        /// 选择合同类型
        /// </summary>
        private Dictionary<string, string> CBMicValueList_;

        public Dictionary<string, string> CBMicValueList
        {
            get
            {
                if(CBMicValueList_ == null || CBMicValueList_.Count == 0)
                {
                    CBMicValueList_ = new Dictionary<string, string>();
                    CBMicValueList_.Add("CBM_Contract", "业主合同");
                    CBMicValueList_.Add("CBM_ContractMaterial", "物资合同");
                    CBMicValueList_.Add("CBM_OtherContract", "分包合同");
                }
                return CBMicValueList_;

            }
            set { CBMicValueList_ = value; }
        }
        private string SelectText_;
        /// <summary>
        /// 选中的合同类型数据
        /// </summary>
        public string SelectText
        {
            get { return SelectText_; }
            set { SelectText_ = value; }
        }

        private List<CBM_Contract> CBM_ContractList_;

        public List<CBM_Contract> CBM_ContractList
        {
            get { return CBM_ContractList_; }
            set { CBM_ContractList_ = value;
                SetProperty(ref CBM_ContractList_, CBM_ContractList);
            }
        }
        private List<CBM_MaterialPurchaseList> CBM_MaterialPurchaseListDtl_;

        public List<CBM_MaterialPurchaseList> CBM_MaterialPurchaseListDtl
        {
            get { return CBM_MaterialPurchaseListDtl_; }
            set { CBM_MaterialPurchaseListDtl_ = value;
                SetProperty(ref CBM_MaterialPurchaseListDtl_, CBM_MaterialPurchaseListDtl);
            }
        }

        #endregion
    }
}
